<?php

	//klasa do osbługi błędów
	class CErrorBase
	{
		static function &GetInstance()
		{
			global $g_hObjects;
			static $m_hApp = NULL;
						 
			if(!isset($m_hApp))
			{
				$m_hApp = new CErrorBase();					 
				$g_hObjects[] = $m_hApp;
				$m_hApp->OnInit();
			}
			
			return $m_hApp;
		}
		
		
		function OnInit()
		{	
			//ustalamy poziom raportowania bledow
			error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
			
			//istalujemy handler'a obslugujacego bledy
			set_error_handler( array(&$this,'UserErrorHandler') );
		}
		
		
		function OnDestroy()
		{
			
		}

		function UserErrorHandler($errno, $errmsg, $filename, $linenum, $vars)
		{
			$errortype = array(E_ERROR 	=> 'Error',
							   E_WARNING	=> 'Warning',
							   E_PARSE      => 'Parsing Error',
							   E_NOTICE     => 'Notice',
							   E_CORE_ERROR => 'Core Error',
							   E_CORE_WARNING => 'Core Warning',
							   E_COMPILE_ERROR  => 'Compile Error',
							   E_COMPILE_WARNING => 'Compile Warning',
							   E_USER_ERROR => 'User Error',
							   E_USER_WARNING => 'User Warning',
							   E_USER_NOTICE => 'User Notice',
							   E_STRICT => 'Runtime Notice',
							   E_RECOVERABLE_ERROR => 'Catchable Fatal Error');
				   
		
			//if($errno == E_WARNING || $errno == E_NOTICE)
			//    return;
			
			
			$error_type ='';
			
			foreach($errortype as $val => $error_str)
			{
				if( ($errno & $val) != 0)
				{
					$error_type = "$error_str ";
				}
			}	
			
			$szErrorMsg  =  '<p style="color: blue;">';
			$szErrorMsg .=  'System chwilowo niedostepny. Twoje dane sa bezpieczne. ';
			$szErrorMsg .=  'Pracujemy nad rozwiazaniem problemu! </p> <br/><br/>';
			
			
			
			
			echo $szErrorMsg;
			
			$szTime = date("d-m-Y H:i:s");
			
			//$hFile = fopen(LOG_DIR.'/error.log',"a+");
			
			//$hFile = false;
			
			$hFile = false;
			
			if(!$hFile)
			{
				$szMsg = "$szTime => unable to save error to log file! <br>";
				$szMsg.= "$szTime => type: $error_type, file: $filename, line: $linenum <br>";
				$szMsg.= "$szTime => msg: $errmsg <br><br>";
				
				echo $szMsg;
				
				echo '<pre>';
				
				$aTrace = debug_backtrace();
				var_dump($aTrace);
				
				echo '</pre>';
			}
			else
			{
				$szMsg = "$szTime => type: $error_type, file: $filename, line: $linenum \n";
				$szMsg.= "$szTime => msg: $errmsg \n\n";
				
				fwrite($hFile,$szMsg);
				fclose($hFile);
			}
			
			exit();	
		}
	
	}
	
	//installing exception base
	CErrorBase::GetInstance();

?>